package com.yellowpage.wf;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class WorkPanelGenerator {
	VelocityEngine velocityEngine;
	VelocityContext velocityContext;
	
	protected Map properties = null;	
	
	protected List<Map> fieldList = new ArrayList();
	
	public WorkPanelGenerator() {
		velocityEngine = new VelocityEngine();
		velocityContext = new VelocityContext();

		velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);

		try {
			velocityEngine.init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String construct(InputStream input){
        String html = "";
        try {
            StringWriter writer = new StringWriter();
            StringBuffer content = new StringBuffer();

            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            String str = "";
            while (str != null) {
                str = in.readLine();
                if(str != null)
                    content.append(str);
            }

            velocityEngine.evaluate(velocityContext, writer, "", content.toString());

            html = writer.getBuffer().toString();
            writer.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return html;
    }
	
	public String construct(String input){
        String html = "";
        try {
            StringWriter writer = new StringWriter();           

            velocityEngine.evaluate(velocityContext, writer, "", input);

            html = writer.getBuffer().toString();
            writer.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return html;
    }

    public void setObject(String key, Object obj){
        velocityContext.put(key, obj);
    }
    
    public static void main(String[] args){
    	WorkPanelGenerator gene = new WorkPanelGenerator();
    	
    }
		

}
